package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.query.StrategyThemeQuery;
import cn.wolfcode.wolf2w.repostiory.StrategyThemeRepository;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class StrategyThemeServiceImpl implements IStrategyThemeService {

    @Autowired
    private StrategyThemeRepository repository;

    @Autowired
    private MongoTemplate template;

    @Override
    public StrategyTheme get(String id) {
        Optional<StrategyTheme> optional = repository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }

    @Override
    public Page<StrategyTheme> query(StrategyThemeQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize(),
                Sort.Direction.ASC,"_id");
        return DBHelper.query(template, StrategyTheme.class, query, pageable);
    }
    @Override
    public void saveOrUpdate(StrategyTheme theme) {
        if(!StringUtils.hasLength(theme.getId())){
            //添加
            theme.setId(null);
            repository.save(theme);
        }else{

            repository.save(theme);



        }
    }
    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }
    @Override
    public List<StrategyTheme> list() {
        return repository.findAll();
    }
}
